import { Component, OnInit, OnDestroy, Input, Output,SimpleChanges, EventEmitter} from '@angular/core';

/**
 * 使用方式：
 * <bt-region-match [(type)]="type" [(code)] = "code"></bt-region-match>
 *
 */
@Component({
    selector: 'bt-region-match',
    templateUrl: 'bt-region-match.component.html',
})
export class BtRegionMatchComponent implements OnInit, OnDestroy {
    @Input() type;
    @Input() code;
    @Output() typeChange = new EventEmitter<any>();
    @Output() codeChange = new EventEmitter<any>();
    constructor(public btDataService) {
    }

    ngOnChanges(changes: SimpleChanges): void{
        if(changes.hasOwnProperty('code') || changes.hasOwnProperty('type')){
            if(!this.code || !this.type){
                return;
            }
            if(this.type == 1){
                this.getCountryList(this.code);
            }else if(this.type == 2){
                this.getLeagueList(this.code,1);
            }
        }
    }

    ngOnInit() {
    }

    countryList;
    getCountryList(code){
        this.btDataService.getBkCountryList(code)
            .then(res=>{
                if(res.hasOwnProperty('code') && res.code == '1'){
                    this.countryList = res.countryList;
                    return;
                }
                console.error("获取赛事国家列表失败!");
            }).catch(e=>{
            console.error("获取赛事国家接口异常：",e);
        });
    }

    leagueList;
    getLeagueList(code,pageNo){
        this.btDataService.getBkLeagueList(code,pageNo)
            .then(res=>{
                if(res.hasOwnProperty('code') && res.code == '1'){
                    this.leagueList = res.leagueList;
                    return;
                }
                console.error("获取联赛列表失败!");
            }).catch(e=>{
            console.error("获取联赛接口异常：",e);
        });
    }

    ngOnDestroy() {

    }
}
